const Koa = require("koa");
const server = new Koa();
const router = require("koa-router")();
const parser = require("koa-parser");
const cors = require("koa2-cors");
server.use(cors())
server.use(parser());

let studentList = [
    {
        id: "王佳伟",
        password: "123456",
        e_mail: "1951253781@qq.com",
        sex: "男",
        hobby: "篮球"
    },
    {
        id: "王子扬",
        password: "123456",
        e_mail: "1951253781@qq.com",
        sex: "男",
        hobby: "篮球"
    },
    {
        id: "王英杰",
        password: "123456",
        e_mail: "1951253781@qq.com",
        sex: "男",
        hobby: "篮球"
    }
]
router.get("/student", async ctx => {
    ctx.body = studentList
})
router.post("/student", async ctx => {
    let student = ctx.request.body.student;
    studentList.push(student);
    ctx.body = studentList
    ctx.body = true
})
router.delete("/student/:id", async ctx => {
    let id = ctx.params.id;
    console.log(id)
    studentList.splice(id,1);
    ctx.body = true
})
// router.delete("/student/:j", async ctx => {
//     let ind = ctx.params.j;
//     studentList.splice(ind,1);
//     ctx.body = true;
// })
router.put("/student/:index",async ctx=>{
    let ind = ctx.params.index;
    let setUsr = ctx.request.body.student;
    studentList.splice(ind,1,setUsr)
    ctx.body = true
})
router.get("/getMod/:index",async ctx=>{
  let ind = ctx.params.index;
  let tempUsr;
  for(let i=0;i<studentList.length;i++){
    if(ind == i){
      tempUsr = studentList[i]
    }
  }
  ctx.body = tempUsr;
})
server.use(router.routes());
server.listen(3000, () => {
    console.log("3000端口")
})